package com.letian.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.common.constant.Constants;
import com.letian.common.constant.IsConstants;
import com.letian.common.core.domain.entity.SysUser;
import com.letian.common.core.domain.model.UsersVO;
import com.letian.info.domain.InfoUserFollow;
import com.letian.info.service.IInfoUserFollowService;
import com.letian.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.info.domain.InfoArticle;
import com.letian.info.service.IInfoArticleService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 文章信息Controller
 * 
 * @author bmz
 * @date 2023-04-05
 */
@RestController
@RequestMapping("/info/article")
public class InfoArticleController extends BaseController
{
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IInfoArticleService infoArticleService;

    @Autowired
    private IInfoUserFollowService infoUserFollowService;

    /**
     * 查询文章信息列表
     */
    //@PreAuthorize("@ss.hasPermi('info:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoArticle infoArticle)
    {
        startPage();
        if("全部".equals(infoArticle.getClassify()) || "all".equals(infoArticle.getClassify())){
            infoArticle.setClassify("");
        }
        List<InfoArticle> list = infoArticleService.selectInfoArticleList(infoArticle);
        return getDataTable(list);
    }

    /**
     * 查询文章信息列表
     */
    // @PreAuthorize("@ss.hasPermi('info:article:list')")
    @GetMapping("/label/list")
    public TableDataInfo labelList(InfoArticle infoArticle)
    {
        startPage();
        if("全部".equals(infoArticle.getClassify()) || "all".equals(infoArticle.getClassify())){
            infoArticle.setClassify("");
        }
        List<InfoArticle> list = infoArticleService.selectInfoArticleListByLabel(infoArticle,"admin");
        return getDataTable(list);
    }

    /**
     * 查询文章信息列表
     */
    @PreAuthorize("@ss.hasPermi('info:article:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(InfoArticle infoArticle)
    {
        startPage();
        if("全部".equals(infoArticle.getClassify()) || "all".equals(infoArticle.getClassify())){
            infoArticle.setClassify("");
        }
        infoArticle.setAuthorName(getUsername());
        List<InfoArticle> list = infoArticleService.selectInfoArticleListByLabel(infoArticle, getUsername());
        return getDataTable(list);
    }

    /**
     * 查询文章信息列表
     */
    @PreAuthorize("@ss.hasPermi('info:article:list')")
    @GetMapping("/like/list")
    public TableDataInfo likeList(InfoArticle infoArticle)
    {
        startPage();
        List<InfoArticle> list = infoArticleService.likeList(infoArticle, getUsername());
        return getDataTable(list);
    }

    /**
     * 查询文章信息列表 search
     */
    @PreAuthorize("@ss.hasPermi('info:article:list')")
    @GetMapping("/search/list")
    public TableDataInfo list(String title)
    {
        startPage();
        List<InfoArticle> list = infoArticleService.queryArticleList(title, getUsername());
        return getDataTable(list);
    }

    /**
     * 导出文章信息列表
     */
    @PreAuthorize("@ss.hasPermi('info:article:export')")
    @Log(title = "文章信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoArticle infoArticle)
    {
        List<InfoArticle> list = infoArticleService.selectInfoArticleList(infoArticle);
        ExcelUtil<InfoArticle> util = new ExcelUtil<InfoArticle>(InfoArticle.class);
        util.exportExcel(response, list, "文章信息数据");
    }

    /**
     * 获取文章信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('info:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        InfoArticle article = infoArticleService.selectInfoArticleByArticleId(articleId);
        SysUser user = sysUserService.selectUserByUserName(article.getAuthorName());
        if (user != null) {
            UsersVO userVO = new UsersVO();
            BeanUtils.copyProperties(user, userVO);
            article.setAuthorInfo(userVO);


            InfoUserFollow infoUserFollow = new InfoUserFollow();
            infoUserFollow.setUserName(getUsername());
            infoUserFollow.setFollowUserName(article.getAuthorName());
            List<InfoUserFollow> infoUserFollows = infoUserFollowService.selectInfoUserFollowList(infoUserFollow);
            if (infoUserFollows.size() > 1) {
                article.setIsAuthorLike(Constants.SYS_YES);
            } else{
                article.setIsAuthorLike(Constants.SYS_NO);
            }
        }
        return success(article);
    }

    /**
     * 获取文章信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:article:query')")
    @GetMapping(value = "/user/{articleId}")
    public AjaxResult queryUserArticleDetail(@PathVariable("articleId") Long articleId){
        return success(infoArticleService.queryUserArticleDetail(getUsername(),articleId));
    }

    /**
     * 点赞文章, 只能点赞，不能取消
     */
    @PreAuthorize("@ss.hasPermi('info:article:edit')")
    @PutMapping(value = "/user/thumbsup/{articleId}")
    public AjaxResult thumbsUp(@PathVariable Long articleId){
        return success(infoArticleService.thumbsUp(getUsername(),articleId, IsConstants.IS));
    }

    /**
     * 获取文章信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:article:edit')")
    @PutMapping(value = "/user/like/{articleId}")
    public AjaxResult likeArticle(@PathVariable Long articleId){
        return success(infoArticleService.likeArticle(getUsername(),articleId));
    }

    /**
     * 新增文章信息
     */
    @PreAuthorize("@ss.hasPermi('info:article:add')")
    @Log(title = "文章信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoArticle infoArticle)
    {
        infoArticle.setCreateBy(getUsername());
        infoArticle.setAuthorName(getUsername());
        return toAjax(infoArticleService.insertInfoArticle(infoArticle));
    }

    /**
     * 修改文章信息
     */
    @PreAuthorize("@ss.hasPermi('info:article:edit')")
    @Log(title = "文章信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoArticle infoArticle)
    {
        infoArticle.setUpdateBy(getUsername());
        return toAjax(infoArticleService.updateInfoArticle(infoArticle));
    }

    /**
     * 删除文章信息
     */
    @PreAuthorize("@ss.hasPermi('info:article:remove')")
    @Log(title = "文章信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(infoArticleService.deleteInfoArticleByArticleIds(articleIds));
    }
}
